<?php

namespace app\admin\controller;

//消息管理
class Message extends Common
{
    /**
     * 消息列表
     * @return mixed
     * @throws \think\Exception
     */
    function message_list(){
        $where="a.id > 0";
        $name=Request()->param('name');
        $status=Request()->param('status');
        $start_time=Request()->param('start_time');
        $end_time=Request()->param('end_time');
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
        if($name!=''){
            $where.=" and a.title like '%".$name."%'";
        }
        if($status>0){
            if($status==1){
                $where.=" and a.status = 1";
            }else{
                $where.=" and a.status != 1";
            }

        }
        if(strtotime($start_time)>946656000){
            $start_time=strtotime($start_time);
            $where.=" and a.create_time >= $start_time";
        }else{
            $start_time=0;
        }
        if(strtotime($end_time)>946656000){
            $end_time=strtotime($end_time);
            $where.=" and a.create_time <= $end_time";
        }else{
            $end_time=0;
        }
        $data=db('message a')
            ->field("a.id,a.title,a.create_time,a.status,a.add_user_name,a.user_id,b.u_mobile")
            ->join("fr_user b","a.user_id = b.u_id","left")
            ->where($where)
            ->limit("$now,$size")
            ->order("a.id desc")
            ->select();
        $count=db('message a')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($data));//分页
        $this->assign('data',$data);
        $this->assign('count',$count);
        $this->assign('name',$name);
        $this->assign('status',$status);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('page_str',$page_str);
        return $this->fetch();
    }
    //添加消息
    function add_message(){
        if(Request()->isPost()){
            $data=Request()->post();
            $data['title']=trim($data['title']);
            //问题标题
            $data['mobile']=trim($data['mobile']);
            //正对用户
            if($data['title']==''){
                return return_json(array('code'=>0,'msg'=>'标题不能为空'),'添加消息');
            }
            if($data['editorValue']==''){
                return return_json(array('code'=>0,'msg'=>'请输入详情'),'添加消息');
            }
            $data['content']=$data['editorValue'];
            unset($data['editorValue']);
            if($data['mobile']==''){
                $data['user_id']=0;
            }else{
                $user=db("user")
                    ->where(array("u_mobile"=>$data['mobile']))
                    ->find();
                if(empty($user)){
                    return return_json(array('code'=>0,'msg'=>'没有该用户'),'添加消息');
                }
                $data['user_id']=$user['u_id'];
            }
            unset($data['mobile']);
            $problem=db('message')->where(array('title'=>$data['title']))->find();
            if(!empty($problem)){
                return return_json(array('code'=>0,'msg'=>'已有该标题的消息'),'添加消息');
            }
            $data['create_time']=time();
            $data['add_user_id']=session('uid');
            $data['add_user_name']=session('user.real_name');
            $res=db('message')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'消息添加成功'),'添加消息');
            }else{
                return return_json(array('code'=>0,'msg'=>'消息添加失败'),'添加消息');
            }
        }else{
            return $this->fetch();
        }
    }
    //消息冻结
    function message_del(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $res=db('message')
                ->where(array('id'=>$id))
                ->update(array('status'=>2));
            if($res){
                return return_json(array('code'=>1,'msg'=>'冻结成功！'),'冻结消息');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'冻结失败！'),'冻结消息');die;
            }
        }
    }
    //消息启用
    function message_use(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $res=db('message')
                ->where(array('id'=>$id))
                ->update(array('status'=>1));
            if($res){
                return return_json(array('code'=>1,'msg'=>'启用成功！'),'启用消息');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败！'),'启用消息');die;
            }
        }
    }
    //修改消息
    function remessage(){
        if(Request()->isPost()){
            $data=Request()->post();
            $id=$data['id'];
            unset($data['id']);
            $data['title']=trim($data['title']);
            //问题标题
            $data['mobile']=trim($data['mobile']);
            //正对用户
            if($data['title']==''){
                return return_json(array('code'=>0,'msg'=>'标题不能为空'),'添加消息');
            }
            if($data['editorValue']==''){
                return return_json(array('code'=>0,'msg'=>'请输入详情'),'添加消息');
            }
            $data['content']=$data['editorValue'];
            unset($data['editorValue']);
            if($data['mobile']==''){
                $data['user_id']=0;
            }else{
                $user=db("user")
                    ->where(array("u_mobile"=>$data['mobile']))
                    ->find();
                if(empty($user)){
                    return return_json(array('code'=>0,'msg'=>'没有该用户'),'添加消息');
                }
                $data['user_id']=$user['u_id'];
            }
            unset($data['mobile']);
            $problem=db('message')->where(array('title'=>$data['title'],'id'=>array('neq',$id)))->find();
            if(!empty($problem)){
                return return_json(array('code'=>0,'msg'=>'已有该标题的消息'),'添加消息');
            }
            if($data['status']!=1){
                $data['end_time']=time();
            }
            $data['create_time']=time();
            $data['add_user_id']=session('uid');
            $data['add_user_name']=session('user.real_name');
            $res=db('message')->where(array("id"=>$id))->update($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'消息添加成功'),'添加消息');
            }else{
                return return_json(array('code'=>0,'msg'=>'消息添加失败'),'添加消息');
            }
        }else{
            $id=request()->get('id');
            $message=db("message a")
                ->field("a.*,b.u_mobile")
                ->join("fr_user b","a.user_id = b.u_id","left")
                ->where(array("a.id"=>$id))
                ->find();
            $this->assign('id',$id);
            $this->assign('message',$message);
            return $this->fetch();
        }
    }
}